<?php
//Ten file muon xuat ra de in

//DatCQ Chon in giay trinh ky theo dinh dang 20/10/2008
$v_export_type = 1;
if(isset($_REQUEST['hdn_exporttype'])){
	$v_export_type = $_REQUEST['hdn_exporttype'];
}
//End DatCQ Chon in giay trinh ky theo dinh dang 20/10/2008

$v_recordtype_filter = $_SESSION['onegate_current_recordtype'];
if(isset($_REQUEST['hdn_recordtype_filter'])){
	$v_recordtype_filter = $_REQUEST['hdn_recordtype_filter'];
}
//print_r($arr_single_record);
$v_recordtype_filter = $arr_single_record[FK_RECORDTYPE];
$v_record_id_list = '';
if(isset($_REQUEST['hdn_record_id'])){
	$v_record_id_list = $_REQUEST['hdn_record_id'];
}

$v_file_name = "mau_in_giay_trinh_ky.htm";
//file chua cac the can thay the
$v_receipt_xml_file = "../../xml/".$v_recordtype_filter."/mau_in_giay_trinh_ky.xml";
$v_receipt_richtext_file = "../../template/".$v_recordtype_filter."/mau_in_giay_trinh_ky.htm";
if (!file_exists($v_receipt_richtext_file)){
	$v_receipt_richtext_file = "../../template/mau_in_giay_trinh_ky.htm";
}
//echo $v_receipt_richtext_file ;
if (file_exists($v_receipt_xml_file)){
$v_receipt_xml_string = _read_file($v_receipt_xml_file);
}else{
	$v_receipt_xml_file = "../../xml/other/mau_in_giay_trinh_ky.xml";
	$v_receipt_xml_string = _read_file($v_receipt_xml_file);
}
//echo $v_receipt_xml_file;
//var_dump($v_arr);
$table_struct_rax = new RAX(); 
$table_struct_rec = new RAX(); 
$table_struct_rax->open($v_receipt_xml_string);
$table_struct_rax->record_delim = 'str_sql';
$table_struct_rax->parse();
$table_struct_rec = $table_struct_rax->readRecord();
$table_struct_row = $table_struct_rec->getRow();
$str_sql = $table_struct_row["sql"];
$str_sql_replace = $table_struct_row["str_sql_replace"];

//echo $str_sql; exit;
$str_sql = str_replace("#".$str_sql_replace."#",$v_record_id_list,$str_sql);
$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
$v_arr= $ado_conn->GetRow($str_sql); 

//echo $str_sql."<br>"; 
//var_dump($v_arr);exit;
//echo '<br/>'._XML_get_xml_tag_value($v_arr['C_RECEIVED_RECORD_XML_DATA'],"doc_another","doc_another");

/*
************************************************************************************************
dung cap moi va cap tam cho thu tuc 009


**************************************************************************************************
*/
//print_r($v_arr);exit;
	$tam='';
	if($v_recordtype_filter=="009" && _XML_get_xml_tag_value($v_arr['C_RECEIVED_RECORD_XML_DATA'],"record_form","record_form")=="CT")
    	$tam = '_tam';
	$v_receipt_richtext_file = "../../template/".$v_recordtype_filter."/mau_in_giay_trinh_ky".$tam.".htm";
	$v_file_name = "mau_in_giay_trinh_ky".$tam.".htm";

$v_xml_str = $v_arr['C_RECEIVED_RECORD_XML_DATA'];
if ($v_xml_str ==''){
	$v_xml_str = '<?xml version="1.0" encoding="UTF-8"?><root><data_list></data_list></root>';
}
//echo $v_receipt_richtext_file;
$v_receipt_richtext_string = _read_file($v_receipt_richtext_file);

$v_richtext_output = Print_Receipt_From_XML($v_receipt_xml_string,$v_receipt_richtext_string,$v_xml_str,$v_arr);

//=======================================================
//DatCQ Chon in giay phep theo dinh dang 17/10/2008
//Xuat ra file theo dinh dang html
$v_style_file = "license_style.css";
$v_str_style = _read_file($v_style_file);
//Phan dau cua chuoi html
$v_html_content = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
					xmlns:x="urn:schemas-microsoft-com:office:excel">
					<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
$v_html_content = $v_html_content.'<style type=text/css><!--'.$v_str_style.'--></style>';
//Phan dinh dang cac bang cua Exel
$v_html_content = $v_html_content.'<!--[if gte mso 9]><xml>
									 <x:ExcelWorkbook>
									  <x:ExcelWorksheets>
									   <x:ExcelWorksheet>
										<x:Name>Report</x:Name>
										<x:WorksheetOptions>
										 <x:Print>
										  <x:ValidPrinterInfo/>
										  <x:HorizontalResolution>600</x:HorizontalResolution>
										  <x:VerticalResolution>600</x:VerticalResolution>
										 </x:Print>
										 <x:PageBreakZoom>100</x:PageBreakZoom>
										 <x:Selected/>
										 <x:Panes>
										  <x:Pane>
										   <x:Number>0</x:Number>
										   <x:ActiveRow>0</x:ActiveRow>
										   <x:ActiveCol>0</x:ActiveCol>
										  </x:Pane>
										 </x:Panes>
										 <x:ProtectContents>False</x:ProtectContents>
										 <x:ProtectObjects>False</x:ProtectObjects>
										 <x:ProtectScenarios>False</x:ProtectScenarios>
										</x:WorksheetOptions>
										<x:Sorting>
										 <x:Sort>ma</x:Sort>
										</x:Sorting>
									   </x:ExcelWorksheet>
									  </x:ExcelWorksheets>
									  <x:WindowHeight>9345</x:WindowHeight>
									  <x:WindowWidth>15180</x:WindowWidth>
									  <x:WindowTopX>120</x:WindowTopX>
									  <x:WindowTopY>60</x:WindowTopY>
									  <x:ProtectStructure>False</x:ProtectStructure>
									  <x:ProtectWindows>False</x:ProtectWindows>
									 </x:ExcelWorkbook>
									</xml><![endif]-->';
$v_html_content = $v_html_content.'</head><body>'.$v_richtext_output.'</body>';
$v_html_content = $v_html_content.'</html>';

switch($v_export_type) {
	case 1;
		$v_export_filename = "license.htm";
		break;
	case 2;
		$v_html_content = str_replace('text/html','application/msword',$v_html_content);	
		echo $v_html_content;
		$v_export_filename = "license.doc";
		break;
	case 3;
		$v_export_filename = "license.xls";
		$v_html_content = str_replace('text/html','application/vnd.ms-excel',$v_html_content);		
		break;
	default:
		$v_export_filename = "license.htm";
		break;	
}

if($v_export_filename == "license.htm") echo $v_html_content;
else{
	_write_file($v_export_filename ,$v_html_content);

	echo '<script language="JavaScript">
		//document.forms(0).submit();
		goto_url("'.$v_export_filename.'",1);
		</script>';
}

//End DatCQ Chon in giay phep theo dinh dang 17/10/2008
//=======================================================
?>